/*
    Project: OCMF (Operant Chamber Management Framework)
    Version: 1.0
    Author: Copyright (C) 2010, Samuel Delaney & Andrew Harvey
    Licence:
        This file is part of OCMF.

            OCMF is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.

            OCMF is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.

            You should have received a copy of the GNU General Public License
            along with OCMF.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ScreenTemplate.h"

ScreenTemplate::ScreenTemplate(QString name)
{
    Name = name;
}

ScreenTemplate::ScreenTemplate(QByteArray screenTemplateBytes)
{
    QDomDocument xmlDocument;
    //set the content of the xmldocument to the file
    if(!xmlDocument.setContent(screenTemplateBytes))return;
    //get the root of the xmldocument
    QDomElement xmlRoot = xmlDocument.documentElement();
    //set member values
    BackColour = *(new QColor(xmlRoot.attributeNode("BackColour").value()));
    AspectRatio = *(new QString(xmlRoot.attributeNode("AspectRatio").value()));
    //get all the children nodes
    QDomNodeList placeholderNodeList = xmlRoot.childNodes();
    //iterate over each placeholder
    for(int i = 0; i < placeholderNodeList.count(); i++)
    {
        Placeholder * placeholder = new Placeholder();
        //get element version
        QDomElement placeholderElement = placeholderNodeList.at(i).toElement();
        //get all the values from the attributes
        placeholder->X = placeholderElement.attributeNode("X").value().toFloat(NULL);
        placeholder->Y = placeholderElement.attributeNode("Y").value().toFloat(NULL);
        placeholder->Width = placeholderElement.attributeNode("Width").value().toFloat(NULL);
        placeholder->Height = placeholderElement.attributeNode("Height").value().toFloat(NULL);
        //add the placeholder to the vector
        Placeholders.push_back(*placeholder);
    }
}

ScreenTemplate::ScreenTemplate(QDomElement screenTemplateElement)
{
        //set member values
    BackColour = *(new QColor(screenTemplateElement.attributeNode("BackColour").value()));
    AspectRatio = *(new QString(screenTemplateElement.attributeNode("AspectRatio").value()));
    //get all the children nodes
    QDomNodeList placeholderNodeList = screenTemplateElement.childNodes();
    //iterate over each placeholder
    for(int i = 0; i < placeholderNodeList.count(); i++)
    {
        Placeholder * placeholder = new Placeholder();
        //get element version
        QDomElement placeholderElement = placeholderNodeList.at(i).toElement();
        //get all the values from the attributes
        placeholder->X = placeholderElement.attributeNode("X").value().toFloat(NULL);
        placeholder->Y = placeholderElement.attributeNode("Y").value().toFloat(NULL);
        placeholder->Width = placeholderElement.attributeNode("Width").value().toFloat(NULL);
        placeholder->Height = placeholderElement.attributeNode("Height").value().toFloat(NULL);
        //add the placeholder to the vector
        Placeholders.push_back(*placeholder);
    }
}

ScreenTemplate::~ScreenTemplate()
{
    delete &BackColour;
    delete &AspectRatio;
    //delete all dynamically allocated placeholders in the vector
    for(int i = 0; i < Placeholders.count(); i++)
        delete &Placeholders.at(i);
}
